package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.google.gson.Gson
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.GetAdsResponse
import com.kll.carolinehmibiz.net.bean.GetSystemMenuResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.HomePageProtocol

class HomePagePresenter(context: Context, mView: HomePageProtocol.IView): BasePresenter(context), HomePageProtocol.IPresenter {
    private var mView: HomePageProtocol.IView? = mView
    private var getHomePageAdsIsBusying = false
    private var data: Any? = null

    override fun getHomePageAds() {
        if (getHomePageAdsIsBusying) return
        getHomePageAdsIsBusying = true
        OperationManager.getInstancce().getHomePageAds(object : OperateListener {


            override fun onSuccess(state: Int, obj: Any?) {
                getHomePageAdsIsBusying = false
                if (state == 200) {
                    if(data == null || !Gson().toJson(data).equals(Gson().toJson(obj))) {
                        data = obj
                        mView?.getHomePageAdsSuccessed(data!! as GetAdsResponse)
                    }
                }
            }

            override fun onFail(code: Int, message: String) {
                getHomePageAdsIsBusying = false
                mView?.getHomePageAdsFailed(code, message)
            }

        })
    }

    override fun loadSystemMenu(parentId: Int) {
        OperationManager.getInstancce().getSystemAllMenus(parentId, object: OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                if (state == 200) {
                    mView?.getSystemMenuSuccessed(obj as ArrayList<GetSystemMenuResponse>)
                }
            }
            override fun onFail(code: Int, message: String) {
                mView?.getSystemMenuFailed(code, message)
            }

        })
    }

}